package com.sqlcheck.dto.response;

import com.sqlcheck.entity.CompatibilityRule;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.time.LocalDateTime;

@Data
@Schema(description = "兼容性规则响应")
public class CompatibilityRuleResponse {

    @Schema(description = "规则ID", example = "1")
    private Long id;

    @Schema(description = "规则标识", example = "ORACLE_CHAR_001")
    private String ruleId;

    @Schema(description = "规则名称", example = "CHAR长度超限")
    private String ruleName;

    @Schema(description = "规则分类", example = "DATA_TYPE")
    private String category;

    @Schema(description = "源数据库类型", example = "ORACLE")
    private String sourceDbType;

    @Schema(description = "目标数据库类型", example = "GOLDENDB")
    private String targetDbType;

    @Schema(description = "规则模式", example = "CHAR\\s*\\(\\s*(\\d+)\\s*\\)")
    private String rulePattern;

    @Schema(description = "严重程度", example = "HIGH")
    private String severity;

    @Schema(description = "规则描述", example = "CHAR长度超过GoldenDB最大支持长度255")
    private String description;

    @Schema(description = "修改建议", example = "将CHAR类型改为VARCHAR类型，或减少长度到255以内")
    private String suggestion;

    @Schema(description = "示例SQL", example = "CHAR(1000)")
    private String exampleSql;

    @Schema(description = "建议SQL", example = "VARCHAR(1000)")
    private String suggestedSql;

    @Schema(description = "是否启用", example = "true")
    private Boolean isActive;

    @Schema(description = "创建时间")
    private LocalDateTime createdAt;

    @Schema(description = "更新时间")
    private LocalDateTime updatedAt;

    public static CompatibilityRuleResponse fromEntity(CompatibilityRule entity) {
        CompatibilityRuleResponse response = new CompatibilityRuleResponse();
        response.setId(entity.getId());
        response.setRuleId(entity.getRuleId());
        response.setRuleName(entity.getRuleName());
        response.setCategory(entity.getCategory());
        response.setSourceDbType(entity.getSourceDbType() != null ? entity.getSourceDbType().name() : null);
        response.setTargetDbType(entity.getTargetDbType() != null ? entity.getTargetDbType().name() : null);
        response.setRulePattern(entity.getRulePattern());
        response.setSeverity(entity.getSeverity() != null ? entity.getSeverity().name() : null);
        response.setDescription(entity.getDescription());
        response.setSuggestion(entity.getSuggestion());
        response.setExampleSql(entity.getExampleSql());
        response.setSuggestedSql(entity.getSuggestedSql());
        response.setIsActive(entity.getIsActive());
        response.setCreatedAt(entity.getCreatedAt());
        response.setUpdatedAt(entity.getUpdatedAt());
        return response;
    }
}
